package com.gxa.zhonglong.controller;

import com.gxa.zhonglong.common.ExceptionCode;
import com.gxa.zhonglong.common.JsonResult;
import com.gxa.zhonglong.exception.BusinessException;
import com.gxa.zhonglong.pojo.Headquarters;
import com.gxa.zhonglong.service.CounterService;
import com.gxa.zhonglong.service.HeadquartersService;
import com.gxa.zhonglong.service.SubbranchService;
import com.gxa.zhonglong.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @author : 宸濯
 * Date: 2021/10/25 14:21
 * Description:总部操作接口
 * Version: V1.0
 */
@Api(tags ="总部接口api")
@CrossOrigin
@RestController
@RequestMapping("/headquarters")
public class HeadquartersController {

    @Autowired
    private HeadquartersService headquartersService;
    @Autowired
    private SubbranchService subbranchService;
    @Autowired
    private CounterService counterService;

    @PostMapping("/selectIdAndName")
    @ApiOperation("查询所有总部(id,name信息)")
    public Map<String,Object> selectIdAndName(){
        List<Map<String, Object>> maps = headquartersService.selectIdAndName();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(maps);
        return jsonResult.getDataMap();
    }

    @PostMapping("/directory")
    @ApiOperation("门店目录树")
    public Map<String,Object> directory(){
        List<Map<String, Object>> headquarters = headquartersService.selectIdAndName();
        //遍历每个总店
        for (Map<String, Object> stringObjectMap : headquarters) {
            List<Map<String, Object>> subbranch = subbranchService.selectIdAndName((Long) stringObjectMap.get("id"));
            if (subbranch.size() > 0) {
                //遍历某个总店下的分店
                for (Map<String, Object> map : subbranch) {
                    //找到某个分店下的柜台
                    List<Map<String, Object>> counts = counterService.selectIdAndName((Long) map.get("id"));
                    //将柜台添加到分店树
                    map.put("children", counts);
                }
                //将分店添加到总店树
                stringObjectMap.put("children", subbranch);
            }

        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(headquarters);
        return jsonResult.getDataMap();
    }

    @PostMapping("/selectAll")
    @ApiOperation("查询所有总部")
    public Map<String,Object> selectAll(){
        List<Map<String, Object>> maps = headquartersService.selectAll();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(maps);
        return jsonResult.getDataMap();
    }

    @PostMapping("/selectById")
    @ApiOperation("根据id查询总部信息")
    public Map<String,Object> selectById(Long id) throws BusinessException {
        Map<String, Object> stringObjectMap = headquartersService.selectById(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(stringObjectMap);
        return jsonResult.getDataMap();
    }

    @PostMapping("/addOrUpdate")
    @ApiOperation("添加/修改总部")
    public Map<String,Object> add(Headquarters headquarters) throws BusinessException {
        if(!StringUtil.isEmpty(headquarters.getPhone()) && !StringUtil.isPhoneNumber(headquarters.getPhone())){
            throw new BusinessException(ExceptionCode.BAD_PHONE);
        }
        JsonResult jsonResult = new JsonResult();
        //id为空添加总部
        if(headquarters.getId() == null){
            headquartersService.add(headquarters);
            jsonResult.setMsg("添加成功!");

        }else {
            //id不为空修改总部
            headquartersService.update(headquarters);
            jsonResult.setMsg("修改成功!");
        }
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }


    @PostMapping("/del")
    @ApiOperation("根据id删除总部")
    public Map<String,Object> del(Long id) throws BusinessException {
        headquartersService.del(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("删除成功!");
        return jsonResult.getDataMap();
    }
}
